package nqueens;

import java.util.*;
import java.util.concurrent.*;
import nqueens.algorithm.*;

/**
 *
 * @author Scott
 */
public class Main extends Thread {

    private final static int TIMER = 60;

    /**
     * private helper method for delaying the main thread while the algorithms process
     * @param interval number of seconds to be delayed
     */
    private static void sleep (int interval)  {
        try {
            Thread.sleep(interval * 1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
//        // warn the user before continuing
        System.out.println("THIS PROGRAM WILL STRESS YOUR CPU, PLEASE SAVE ANYTHING IMPORTANT AND \n" +
                "CLOSE ANYTHING PROCESSOR INTENSIVE BEFORE CONTINUING!");
        System.out.println();
        System.out.println("Are you sure you wish to continue (y/n)?");
        String s = new Scanner (System.in).nextLine();
        s = s.trim();
        if (s.startsWith("y") || s.startsWith("Y")) {
//             this runs much, much faster when the flag is false, which means no error checking
            Algorithm ginat = new DrGinatAlgorithm(false);
            Algorithm levitin = new DrLevitin(false);
            Algorithm exhaustive = new ExhaustiveAlgorithm();
            Algorithm backtracking = new BacktrackingAlgorithm(false);

            ExecutorService service = Executors.newCachedThreadPool();

            service.execute(ginat);
            service.execute(levitin);
            service.execute(exhaustive);
            service.execute(backtracking);

            for (int counter = 0, interval = 5; counter < TIMER; counter+=interval) {
                System.out.println("Time Elapsed: " + counter + " seconds");
                sleep(interval);
                if (counter+interval < TIMER) {
                    comedy();
                }
            }

            System.out.println("");
            System.out.println("Dr Ginat's Algorithm yielded a solution for all values of N up to and including N = " +
                    ginat.getLastSolutionSolved().getN());
            System.out.println("Dr Levitin's Algorithm yielded a solution for all values of N up to and including N = " +
                    levitin.getLastSolutionSolved().getN());
            System.out.println("The Exhaustive Search Algorithm yielded a solution for all values of N up to and including N = " +
                    exhaustive.getLastSolutionSolved().getN());
            System.out.println("The Backtracking Algorithm yielded a solution for all values of N up to and including N = " +
                    backtracking.getLastSolutionSolved().getN());

            service.shutdownNow();

            //System.exit(0);
        }
    }

    /**
     * This is simply for to add some light humor to the timed tests
     */
    private static void comedy() {
        Random r = new Random();
        switch (r.nextInt(10)) {
            case 0:
            case 1:
            case 2:
                break;
            case 3:
                System.out.println("...Wow, this is taking a lot of time, wish I was more patient.");
                break;
            case 4:
                System.out.println("Look's like I shouldn't have bet on exhaustive search...");
                break;
            case 5:
                System.out.println("Dr Levitin's 4th solution is pretty clever, he must be a smart guy (who says flattery gets you nowhere).");
                break;
            case 6:
                System.out.println("Look at those Queens go!");
                break;
            case 7:
                System.out.println("Are we there yet?");
                break;
            case 8:
                System.out.println("I asked a friend for some pointers with the backtracking algorithm, and she responded '0x0A35F4892EC98D35'.  I hate people.");
                break;
            case 9:
                System.out.println("You can do it, you're almost there, just hang on.");
                break;
        }
    }
}
